home *** CD-ROM | disk | FTP | other *** search
/ Languguage OS 2 / Languguage OS II Version 10-94 (Knowledge Media)(1994).ISO / gnu / glibc108.zip / glibc108 / manual / examples / filecli.c < prev    next >
C/C++ Source or Header  |  1994-02-16  |  1KB  |  55 lines

  1. #include <stdio.h>
  2. #include <errno.h>
  3. #include <unistd.h>
  4. #include <stdlib.h>
  5. #include <sys/socket.h>
  6. #include <sys/un.h>
  7.  
  8. #define SERVER    "/tmp/serversocket"
  9. #define CLIENT    "/tmp/mysocket"
  10. #define MAXMSG    512
  11. #define MESSAGE    "Yow!!! Are we having fun yet?!?"
  12.  
  13. int
  14. main (void)
  15. {
  16.   extern int make_named_socket (const char *name);
  17.   int sock;
  18.   char message[MAXMSG];
  19.   struct sockaddr_un name;
  20.   size_t size;
  21.   int nbytes;
  22.  
  23.   /* Make the socket. */
  24.   sock = make_named_socket (CLIENT);
  25.  
  26.   /* Initialize the server socket address. */
  27.   name.sun_family = AF_UNIX;
  28.   strcpy (name.sun_path, SERVER);
  29.   size = strlen (name.sun_path) + sizeof (name.sun_family);
  30.  
  31.   /* Send the datagram. */
  32.   nbytes = sendto (sock, MESSAGE, strlen (MESSAGE) + 1, 0,
  33.            (struct sockaddr *) & name, size);
  34.   if (nbytes < 0)
  35.     {
  36.       perror ("sendto (client)");
  37.       exit (EXIT_FAILURE);
  38.     }
  39.  
  40.   /* Wait for a reply. */
  41.   nbytes = recvfrom (sock, message, MAXMSG, 0, NULL, 0);
  42.   if (nbytes < 0)
  43.     {
  44.       perror ("recfrom (client)");
  45.       exit (EXIT_FAILURE);
  46.     }
  47.  
  48.   /* Print a diagnostic message. */
  49.   fprintf (stderr, "Client: got message: %s\n", message);
  50.  
  51.   /* Clean up. */
  52.   remove (CLIENT);
  53.   close (sock);
  54. }
  55.